package com.GUIWork;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import javax.swing.JButton;

import com.dbConnection.theConnection;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;

public class postItem extends JFrame {
	private JTextField iName;
	private JTextField start;
	private JTextField minpricefield;
	private JTextField buyitnow;
	private int NUMBER_CAT = 1;
	theConnection conn;
	public postItem(theConnection conn) {
		this.conn = conn;
		setTitle("Post A New Item");
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		iName = new JTextField();
		iName.setBounds(143, 33, 268, 20);
		panel.add(iName);
		iName.setColumns(10);
		
		final JTextPane desc = new JTextPane();
		desc.setBounds(143, 76, 268, 64);
		panel.add(desc);
		
		JComboBox catBox = new JComboBox();
		catBox.setBounds(143, 154, 99, 20);
		conn.askDB("SELECT Name FROM Category");
		ArrayList<String> categories = new ArrayList<String>();
		categories.add("FAILED");
		try {
			categories = conn.getLastQuery(1);
		} catch (SQLException e) {
			System.out.println("Categories doesn't exist in the db");
			e.printStackTrace();
		}
		for(int i = 0; i<NUMBER_CAT;i++){
			catBox.addItem(categories.get(i));
		}
		panel.add(catBox);
		
		JComboBox condBox = new JComboBox();
		condBox.setBounds(143, 184, 99, 20);
		condBox.addItem("New");
		condBox.addItem("Very Good");
		condBox.addItem("Good");
		condBox.addItem("Fair");
		condBox.addItem("Poor");
		panel.add(condBox);
		
		start = new JTextField();
		start.setBounds(143, 215, 86, 20);
		panel.add(start);
		start.setColumns(10);
		
		minpricefield = new JTextField();
		minpricefield.setBounds(143, 249, 86, 20);
		panel.add(minpricefield);
		minpricefield.setColumns(10);
		
		JComboBox auctionends = new JComboBox();
		auctionends.setBounds(143, 280, 86, 20);
		auctionends.addItem("1 Day");
		auctionends.addItem("3 Days");
		auctionends.addItem("5 Days");
		panel.add(auctionends);
		
		buyitnow = new JTextField();
		buyitnow.setBounds(143, 311, 86, 20);
		panel.add(buyitnow);
		buyitnow.setColumns(10);
		
		JCheckBox returns = new JCheckBox("");
		returns.setHorizontalAlignment(SwingConstants.RIGHT);
		returns.setBounds(143, 338, 21, 21);
		panel.add(returns);
		
		JLabel lblItemName = new JLabel("Item name");
		lblItemName.setBounds(20, 36, 90, 14);
		panel.add(lblItemName);
		
		JLabel lblDescription = new JLabel("Description");
		lblDescription.setBounds(20, 76, 90, 14);
		panel.add(lblDescription);
		
		JLabel lblCondition = new JLabel("Condition");
		lblCondition.setBounds(20, 187, 90, 14);
		panel.add(lblCondition);
		
		JLabel lblCategory = new JLabel("Category");
		lblCategory.setBounds(20, 157, 90, 14);
		panel.add(lblCategory);
		
		JLabel lblStartingBid = new JLabel("Starting Bid $");
		lblStartingBid.setBounds(25, 215, 86, 14);
		panel.add(lblStartingBid);
		
		JLabel lblMinimumSalePrice = new JLabel("Minimum Sale Price  $");
		lblMinimumSalePrice.setBounds(10, 249, 134, 14);
		panel.add(lblMinimumSalePrice);
		
		JLabel lblAuctionEndsIn = new JLabel("Auction Ends In");
		lblAuctionEndsIn.setBounds(25, 283, 90, 14);
		panel.add(lblAuctionEndsIn);
		
		JLabel lblGetItNow = new JLabel("Get It Now Price  $");
		lblGetItNow.setBounds(22, 314, 111, 14);
		panel.add(lblGetItNow);
		
		JLabel lblReturnsAccepted = new JLabel("Returns Accepted?");
		lblReturnsAccepted.setBounds(7, 338, 130, 14);
		panel.add(lblReturnsAccepted);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setBounds(182, 357, 99, 64);
		panel.add(splitPane);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//TODO get all information
			/*	String name = iName.getText();
			 	String description = desc.getText();
			 	String category = (String)catBox.getSelectedItem();
			 	String condition = (String)condBox.getSelectedItem();
			 	String startbid = start.getText();
			 	String minprice = minpricefield.getText();
			 	String enddate = ????;
			 	String getitprice = buyitnow.getText();
			 	String returnable = "false";
			 	if(returns.isSelected()){
			 		returnable = true;
			 	}
			 	
			 	
			 */
				String query = "INSERT INTO Item VALUES (" + "FINDITEMID, ?, FINDUSERNAME, ?, ?, ?, ?, ?, ?, ?, ?";
				
				//TODO Add it to DB
			}
		});
		splitPane.setLeftComponent(btnSubmit);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//TODO Return user to homepage
				//TODO Possibly make previous frame part of constructor to ease return?
			}
		});
		splitPane.setRightComponent(btnCancel);
	}
}
